package com.sysu.simulation.fragments.sim;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sysu.simulation.BaseFragment;
import com.sysu.simulation.R;
import com.sysu.simulation.SimFragment;
import com.sysu.simulation.bean.SimParams;
import com.sysu.simulation.bean.User;
import com.sysu.simulation.helper.AccountHelper;
import com.sysu.simulation.interfaces.SeriesFragment;

import java.util.Map;

import butterknife.BindView;

public class SimBodyFragment extends BaseFragment implements SeriesFragment, SimFragment.SimParamsSetter{

    @BindView(R.id.et_sim_height)
    EditText mHeight;
    @BindView(R.id.et_sim_weight)
    EditText mWeight;
    @BindView(R.id.rg_sim_gender)
    RadioGroup mGender;
    @BindView(R.id.radio_sim_male)
    RadioButton mMale;
    @BindView(R.id.radio_sim_female)
    RadioButton mFemale;

    private User mUser;
    private User mBody;
    public static SimBodyFragment newInstance() {
        return new SimBodyFragment();
    }

    @Override
    protected void initData() {
        super.initData();
        mUser = AccountHelper.getUser();
        mBody = new User();
        mBody.setId(mUser.getId());
        //mBody.setNickname(mUser.getNickname());
        if(mUser.getHeight()!=0) mHeight.setText(String.valueOf(mUser.getHeight()));
        if(mUser.getWeight()!=0) mWeight.setText(String.valueOf(mUser.getWeight()));
        if(mUser.getGender()==0) mMale.setChecked(true);
        else mFemale.setChecked(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sim_body;
    }

    @Override
    public String getPreviousPage() {
        return null;
    }

    @Override
    public String getNextPage() {
        return SimClothesFragment.class.getName();
    }

    @Override
    public String getNavTitle() {
        return "人体设置";
    }

    @Override
    public Boolean canMoveForward() {
        boolean cancel = false;
        View focusView = null;

        mHeight.setError(null);
        mWeight.setError(null);
        String simHeight = mHeight.getText().toString().trim();
        String simWeight = mWeight.getText().toString().trim();
        int simGender = mMale.isChecked() ? 0 : 1;

        if (TextUtils.isEmpty(simWeight)) {
            mWeight.setError(getString(R.string.error_field_required));
            focusView = mWeight;
            cancel = true;
        }

        if (TextUtils.isEmpty(simHeight)) {
            mHeight.setError(getString(R.string.error_field_required));
            focusView = mHeight;
            cancel = true;
        }

        if(cancel) {
            focusView.requestFocus();
            return false;
        }
        else {
            mBody.setHeight(Integer.parseInt(simHeight));
            mBody.setWeight(Integer.parseInt(simWeight));
            mBody.setGender(simGender);
            return true;
        }
    }

    @Override
    public void setParams(SimParams params) {
        params.setUser(mBody);
    }
}
